﻿<?php
    //CONFIGURACION facebook app 
    $fbconfig['appid' ] = "103431709750272";
    $fbconfig['secret'] = "59b67b29f3ea77b79c983dab8b1514c9";

    //CONFIGURACION rutas
    $fbconfig['baseUrl']            =   "http://www.baella.info/2011/fb/viva/top5/";
    $fbconfig['appBaseUrl']         =   "http://apps.facebook.com/vivatopcinco/";
    $fbconfig['fanpageBaseUrl']     =   "http://www.facebook.com/VIVA.GyM";
    $fbconfig['fanpageAppBaseUrl']  =   "http://www.facebook.com/VIVA.GyM?v=app_103431709750272";
    $ruta_imagenes                  =   'http://www.baella.info/2011/fb/viva/top5/images/';    

    //Permisos App Facebook
    //$fbpermisos = "friends_birthday,friends_likes,friends_about_me,user_about_me,user_birthday,user_location,user_online_presence,user_photos,friends_photos,email,publish_stream,read_friendlists,offline_access,publish_stream,sms,publish_actions";
    $fbpermisos = "email,user_about_me";

    
    //CONFIGURACION PUBLICACION EN MURO
    $muro_titulo        = '&iquest;Cu&aacute;l es tu lugar favorito en casa?';
    $muro_descripcion1  = '';
    $muro_descripcion2  = '{*actor*} ha elegido el siguiente Top 5:';
    $muro_link          = $fbconfig['fanpageAppBaseUrl'];
    //$muro_image         = $ruta_imagenes.'icono.jpg';
    $muro_image         = 'http://www.baella.info/2011/fb/viva/top5/images/icono.jpg';
    

    
    /* 
     * If user first time authenticated the application facebook
     * redirects user to baseUrl, so I checked if any code passed
     * then redirect him to the application url 
     * -mahmud
     */
    /*if (isset($_GET['code']))
    {
        header("Location: " . $fbconfig['appBaseUrl']);
        //header("Location: " . $fbconfig['appBaseUrl'].'/index2.php');
        //echo $_GET['code'];
        exit;
    }*/
    if (isset($_GET['code']))//Verifica si la variable ha sido creada
    {
        //echo 'variable creada'; 
        if(!empty($_GET['code']))//Verifica si la variable esta vacia
        {
            //echo 'variable con contenido'; 
            header("Location: " . $fbconfig['appBaseUrl']);
            exit;
        }
    }
    //~~
    
    //
    if (isset($_GET['request_ids'])){
        //user comes from invitation
        //track them if you need
    }
    
    $user            =   null; //facebook user uid
    try
    {
        include_once "facebook.php";
    }
    catch(Exception $o)
    {
        echo 'error de conexi&oacute;n... Su conexi&oacute;n a Internet est&aacute; lenta, intente en unos minutos. (0x0003)';
    }
    // Create our Application instance.
    $facebook = new Facebook(array(
      'appId'  => $fbconfig['appid'],
      'secret' => $fbconfig['secret'],
      'cookie' => true,
    ));

    //Facebook Authentication part
    $user       = $facebook->getUser();
    // We may or may not have this data based 
    // on whether the user is logged in.
    // If we have a $user id here, it means we know 
    // the user is logged into
    // Facebook, but we don�t know if the access token is valid. An access
    // token is invalid if the user logged out of Facebook.
    
    $loginUrl   = $facebook->getLoginUrl(
            array(
                'scope'         => $fbpermisos
            )
    );

    if ($user) {
      try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
      } catch (FacebookApiException $e) {
        //you should use error_log($e); instead of printing the info on browser
        echo 'error de conexi&oacute;n... Su conexi&oacute;n a Internet est&aacute; lenta, intente en unos minutos. (0x0004)';
        $user = null;
      }
    }

    if (!$user) {
        echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
        exit;
    }
    
    //get user basic description
    $userInfo           = $facebook->api("/$user");

    function d($d)
    {
        //echo '<pre>';
        //print_r($d);
        //echo '</pre>';
        echo 'error de conexi&oacute;n... Su conexi&oacute;n a Internet est&aacute; lenta, intente en unos minutos. (0x0005)';
    }
?>
